package com.luhe.midi.mtrk.event.midievent;

import com.luhe.midi.helper.MidiUtil;
import com.luhe.midi.mtrk.event.Event;

import java.io.OutputStream;

/**
 * Midi终止事件
 */
public class FinalEvent extends MidiEvent {

    public FinalEvent(int tick) {
        this.tick = tick;
    }

    public FinalEvent() {
        this.len = 4;
    }


    @Override
    public byte[] writeOutputStream(OutputStream outputStream) throws Exception {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) 0x00;
        bytes[1] = (byte) 0xFF;
        bytes[2] = (byte) 0x2F;
        bytes[3] = (byte) 0x00;
        outputStream.write(bytes);
        return bytes;
    }

    @Override
    public Event readByte(byte[] bytes) {
        int i = MidiUtil.readDynamicIntIndex(bytes);
        FinalEvent finalEvent = new FinalEvent();
        finalEvent.tick = MidiUtil.readDynamicInt(bytes, 0, i + 1);
        return this;
    }
}
